package me.zk;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import org.apache.commons.compress.utils.Lists;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhang kai
 * @Date: 2022/09/21
 * @Description:
 */
public class WordToPdfTest {

    public static void test() {
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();

        Configure config = Configure.newBuilder().bind("list", policy).build();

        List<Map<String, Object>> list = Lists.newArrayList();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> data = new HashMap<>();//通过map存放要填充的数据
            data.put("year", "201" + i);
            data.put("num", 33 + i);
            list.add(data);
        }
        Map<String, Object> data = new HashMap<>();//通过map存放要填充的数据
        data.put("list", list);
        XWPFTemplate template = XWPFTemplate.compile("/Users/zhangkai/ideaproject/practice/practice-pdf/src/main/resources/test.docx", config).render(data);//调用模板，填充数据
        try {
            FileOutputStream out = new FileOutputStream("/Users/zhangkai/ideaproject/practice/practice-pdf/src/main/resources/test1.docx");//要导出的文件名
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (IOException e) {
            e.printStackTrace();

        }

    }

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = new FileInputStream("/Users/zhangkai/ideaproject/practice/practice-pdf/src/main/resources/license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean doc2pdf(String inPath, String outPath) {
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功，共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
    public static void main(String[] args) {
//        WordToPdfTest.test();
        doc2pdf("/Users/zhangkai/ideaproject/practice/practice-pdf/src/main/resources/test.docx",
                "/Users/zhangkai/ideaproject/practice/practice-pdf/src/main/resources/test1.pdf");
    }

}
